package com.blog.config.mq;

import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.AcknowledgeMode;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.config.DirectRabbitListenerContainerFactory;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.connection.Connection;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.concurrent.TimeoutException;

@Configuration
public class RabbitMqConfig {


    @Bean(name = "connectionFactory")
    public ConnectionFactory connectionFactory(@Value("${userName}") String userName,
                                               @Value("${password}") String password,
                                               @Value("${host}") String host,
                                               @Value("${port}") int port,
                                               @Value("${virtualHost}") String virtualHost) {
        CachingConnectionFactory connectionFactory = new CachingConnectionFactory();
        connectionFactory.setUsername(userName);
        connectionFactory.setPassword(password);
        connectionFactory.setHost(host);
        connectionFactory.setPort(port);
        connectionFactory.setVirtualHost(virtualHost);
        return connectionFactory;
    }

    @Resource(name = "connectionFactory")
    private ConnectionFactory connectionFactory;


    /**
     * 队列/交换机/绑定队列与交换机
     *
     * @return
     * @throws IOException
     * @throws TimeoutException
     */
    @Bean
    public void queue() throws IOException, TimeoutException {
        Connection connection = connectionFactory.createConnection();
        // 创建队列
        Channel channel = connection.createChannel(false);
        channel.queueDeclare(QueueStr.TEST_QUEUE, true, false, false, null);
        // 创建交换机
        channel.exchangeDeclare(QueueStr.TEST_DECLARE, ExchangeTypes.TOPIC, false, true, null);
        // 绑定交换机
        channel.queueBind(QueueStr.TEST_QUEUE, QueueStr.TEST_DECLARE, QueueStr.TEST_ROUTER_KEY);
        // 测试发个消息
        byte[] messageBodyBytes = "aaaaa".getBytes();
        channel.basicPublish(QueueStr.TEST_DECLARE, QueueStr.TEST_ROUTER_KEY, null, messageBodyBytes);
    }

    //
    @Bean(name = "testRabbitListener")
    public DirectRabbitListenerContainerFactory rabbitListenerContainerFactory(@Qualifier("connectionFactory") ConnectionFactory connectionFactory) {
        DirectRabbitListenerContainerFactory factory = new DirectRabbitListenerContainerFactory();
        factory.setConnectionFactory(connectionFactory);
        factory.setPrefetchCount(1);
        factory.setAcknowledgeMode(AcknowledgeMode.MANUAL);
        return factory;

    }
////
////    /**
////     * 监听队列
////     */
//    @RabbitListener(queues = QueueStr.TEST_QUEUE, containerFactory = "testRabbitListener")
//    public void getQueue(Message message, Channel channel) throws IOException {
//        byte[] text = message.getBody();
////        channel.basicNack(message.getMessageProperties().getDeliveryTag(),false,false);
//        channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
//    }
}

